<template>
	<view class="main">
		<!-- <view class="search">
			<image src="@/static/img/searchIcon.png" mode=""></image>
			<input type="text" placeholder="请搜索你需要的商品" />
		</view> -->
		<view class="swiper">
			<u-swiper :list="list3" indicator indicatorMode="dot" circular :autoplay="false"></u-swiper>
		</view>
		<view class="info">
			<view class="imgBox">
				<image src="@/static/img/infoIcon.png" mode=""></image>
			</view>
			<view class="hr"></view>
			<view class="msg">
				<text v-if="text.length == 0">暂无公告</text>
				<u-notice-bar v-else :text="text" color="#3d3d3d"></u-notice-bar>
			</view>
			<view class="btn" @tap="routerTo('/pages/pageB/information/infoDetail')">点击查看</view>
		</view>
		<view class="content">
			<h2>热销商品</h2>
			<!-- <p class="tit">优质商品，为你推荐</p> -->
			<u-empty v-if="goodsList.length == 0 && isShow" mode="data"
				icon="http://cdn.uviewui.com/uview/empty/data.png" text="暂无商品"></u-empty>
			<ul class="list">
				<li @tap="routerTo(`/pages/pageB/detail/detail?id=${item.id}`)" v-for="(item, index) in goodsList"
					:key="index">
					<view class="img">
						<easy-loadimage class="image" :scroll-top="scrollTop"
							:image-src="item.image"></easy-loadimage>
						<!-- <image :src="item.image" alt="" /> -->
					</view>
					<view class="txt">
						<p class="h3">{{ item.name }}</p>
						<p class="msg">{{ item.intro }}</p>
						<p class="price">￥{{ item.member_price }}</p>
					</view>
				</li>
			</ul>
		</view>
		<u-modal :show="show" :content="content" @confirm="downLoad" confirmButtonShape="circle"></u-modal>
		<userTabbar seleTabbar="0"></userTabbar>
	</view>
</template>

<script>
	import {
		getBanner,
		getDynamics,
		list,
		getCates,
		getConfigs
	} from '@/api/user.js';
	import userTabbar from '../../components/userTabbar.vue';
	export default {
		components: {
			userTabbar
		},
		data() {
			return {
				isShow: false,
				list3: [],
				data: {
					page_size: 10,
					field: 'is_recom',
					page: 1,
				},
				goodsList: [],
				text: [],
				show: false,
				content: '您的版本不是最新版，请更新后使用',
				version: '',
				last_page:'',
				scrollTop:0
			};
		},
		mounted() {
			if (uni.getStorageSync('enterprise')) {
				uni.redirectTo({
					url: '/pages/index/inner'
				});
			}
		},
		onPageScroll({
			scrollTop
		}) {
			// 传入scrollTop值并触发所有easy-loadimage组件下的滚动监听事件
			this.scrollTop = scrollTop;
		},
		onLoad() {
			/*#ifdef APP-PLUS*/
			// const systemInfo = uni.getSystemInfoSync()
			// if (systemInfo.platform === 'android') {
			//     uni.getSystemInfo({
			//     	success: (res) => {
			//     		this.version = res.appWgtVersion;
			//     		getConfigs({
			//     			name: 'version'
			//     		}).then((res) => {
			//     			console.log(res)
			//     			console.log(this.version)
			//     			if (this.version != res.data.value) {
			//     				this.show = true;
			//     			}
			//     		});
			//     	}
			//     });
			// } 
			/*#endif*/
			this.indexInfo();
			// this.classInfo();
			this.getList()
		},
		onReachBottom() {
			if(this.data.page == this.last_page){
				uni.showToast({
					title:'已经是最后一页了',
					icon:'none'
				})
			}else{
				this.data.page++
				this.getList()
			}
		},
		methods: {
			downLoad() {
				uni.getSystemInfo({
					success: function(res) {
						uni.showLoading({
							title: '下载中'
						});
						uni.downloadFile({
							url: `https://app.guoshangufen.com/gsyy.apk`,
							success: (res) => {
								console.log(res, '下载成功');
								if (res.statusCode === 200) {
									console.log('下载成功');
									uni.hideLoading();
									uni.showToast({
										title: '下载成功',
										icon: 'success'
									});
									uni.saveFile({
										tempFilePath: res.tempFilePath,
										success: function(res) {
											uni.openDocument({
												filePath: res.savedFilePath,
												success: function(res) {
													console.log(res,
														'打开安装包');
												}
											});
										},
										fail: (err) => {
											console.log(err, '打开安装包-失败');
										}
									});
								}
							},
							fail: (err) => {
								console.log(err, '下载失败');
								uni.hideLoading();
								uni.showToast({
									title: '下载失败,请检查网络',
									icon: 'none',
									duration: 1500
								});
							}
						});
					}
				});
			},
			indexInfo() {
				getBanner().then((res) => {
					res.data.forEach((item) => {
						this.list3.push(item.image);
					});
				});
				getDynamics().then((res) => {
					this.text = res.data[0].content
				});
			},
			async getList() {
				if (this.data.page == 1) {
					this.goodsList = []
				}
				this.isShow = false;
				let res = await list(this.data);
				res.data.data.forEach(item => {
					this.goodsList.push(item)
				})
				this.last_page = res.data.last_page
				this.isShow = true;
			},
			routerTo(url) {
				uni.navigateTo({
					url: url
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	//#ifdef APP-PLUS
	.main {
		padding-top: 50px !important;
	}

	//#endif
	.main {
		min-height: calc(100vh - 50px);
		background-image: url('@/static/img/indexBg.png');
		background-repeat: no-repeat;
		background-position: top;
		background-size: 100% 308px;
		background-color: #f8f9fc;
		padding: 25px 15px 60px;

		/deep/.u-notice-bar {
			background-color: #fff !important;
		}

		/deep/.u-notice__left-icon {
			display: none;
		}

		/deep/.u-notice__swiper__item__text {
			color: #3d3d3d !important;
		}

		.search {
			width: 100%;
			border-radius: 10px;
			background-color: #fff;
			display: flex;
			align-items: center;
			padding: 10px 12px;
			margin-bottom: 25px;

			image {
				width: 12px;
				height: 12px;
				margin-right: 7px;
			}

			input {
				flex: 1;
			}
		}

		.swiper {
			margin-bottom: 20px;

			/deep/.u-swiper {
				border-radius: 10px !important;
			}
		}

		.info {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 5px 10px;
			background-color: #fff;
			border-radius: 8px;
			margin-bottom: 15px;

			image {
				width: 30px;
				height: 40px;
			}

			.hr {
				width: 1px;
				height: 20px;
				background-color: #cbc9c9;
				margin: 0 15px 0 10px;
			}

			.msg {
				flex: 1;
				overflow: hidden;
				text-overflow: ellipsis;
				font-size: 12px;
				color: #3d3d3d;
			}

			.btn {
				padding: 5px 15px;
				color: #fff;
				background-color: #0a7ef3;
				font-size: 12px;
				border-radius: 100px;
				margin-left: 20px;
			}
		}

		.content {
			h2 {
				font-size: 16px;
				color: #3d3d3d;
				margin-bottom: 5px;
				line-height: 22px;
			}

			.tit {
				font-size: 12px;
				color: #666666;
				margin-bottom: 15px;
			}

			.list {
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;

				li {
					width: 45%;
					background-color: #fff;
					margin-bottom: 10px;

					.img {
						width: 100%;
						padding-top: 100%;
						margin-bottom: 10px;
						box-sizing: border-box;
						position: relative;
					}

					.image {
						border-radius: 5px 5px 0 0;
						width: 100%;
						height: 100%;
						top: 0;
						left: 0;
						position: absolute;
						object-fit: cover;
					}


					.txt {
						border-radius: 5px;
						padding: 0 7px 15px;

						.h3 {
							width: 100%;
							font-size: 13px;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
							margin-bottom: 8px;
							font-weight: 700;
						}

						.msg {
							font-size: 11px;
							width: 100%;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
							margin-bottom: 10px;
							color: #666666;
						}

						.price {
							color: #eb4a4a;
							font-weight: 700;
						}
					}
				}
			}
		}
	}
</style>